package com.example.axelerometer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.Chronometer;
import android.widget.TextView;

public class Accelerometre extends Activity {
	private SensorManager mSensorManager;
	private TextView mTxtViewX;
	private TextView mTxtViewY;
	private TextView mTxtViewZ;
	private TextView mTxtViewFreq;
    private TextView mTxtViewVx;
    private TextView mTxtViewVy;
    private TextView mTxtViewVz;
    private TextView mTxtViewDx;
    private TextView mTxtViewDy;
    private TextView mTxtViewDz;
    private TextView mTxtViewDist;
    private long previous,now;
    private double pas=0.0;
    private double speed;
    private double distance;
    
    private double Ax=0;
    private double Ay=0;
    private double Az=0;
    
    private double Vx=0;
    private double Vy=0;
    private double Vz=0;
    
    private double Dx=0;
    private double Dy=0;
    private double Dz=0;
    static final float alpha = 0.1f;
	

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mSensorManager = (SensorManager)getSystemService(this.SENSOR_SERVICE); 
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), 
        SensorManager.SENSOR_DELAY_NORMAL);
        mTxtViewX = (TextView) findViewById(R.id.textAx);
        mTxtViewY = (TextView) findViewById(R.id.textAy);
        mTxtViewZ = (TextView) findViewById(R.id.textAz);
        mTxtViewFreq = (TextView) findViewById(R.id.textFreq);
        mTxtViewVx = (TextView) findViewById(R.id.textVx);
        mTxtViewVy = (TextView) findViewById(R.id.textVy);
        mTxtViewVz = (TextView) findViewById(R.id.textVz);
        mTxtViewDist = (TextView) findViewById(R.id.textDx);
        previous=0;
        now=0;
      
    }
    
    public void Position(double pas) 
    { 
    	mTxtViewZ.setText("Az "+Az); 
    	mTxtViewX.setText("Ax "+Ax); 
    	mTxtViewY.setText("Ay "+Ay);
    	
    	mTxtViewFreq.setText("Pas :"+pas);
    	
    	mTxtViewVx.setText("Vx :"+Vx);
    	mTxtViewVy.setText("Vy :"+Vy);
    	mTxtViewVz.setText("Vz :"+Vz);
    	
    	mTxtViewDx.setText("Dx :"+Dx);
    	mTxtViewDy.setText("Dy :"+Dy);
    	mTxtViewDz.setText("Dz :"+Dz);
    	

    }
    
    private final SensorEventListener mSensorListener = new SensorEventListener() { 
        
        public void onSensorChanged(SensorEvent event) 
        { 
            Ax = event.values[0]; 
            Ay = event.values[1]; 
            Az = event.values[2];
            
            //
            // alpha is calculated as t / (t + dT)
            // with t, the low-pass filter's time-constant
            // and dT, the event delivery rate

            //final float alpha = (float) 0.1;
            float gravity[] = new float[3] ;
            float linear[] = new float[3] ;
            gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
            gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
            gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

            Ax = event.values[0] - gravity[0];
            Ay = event.values[1] - gravity[1];
            Az = event.values[2] - gravity[2];
            
            if(Ax<0.05 && Ax > (-0.05)) Ax = 0;
            if(Ay<0.05 && Ay > (-0.05)) Ay = 0;
            if(Az<0.05 && Az > (-0.05)) Az = 0;
            
            previous=now;
        	now=System.currentTimeMillis();
        	computeSpeed();
            Position(pas);
        } 
        
        public void onAccuracyChanged(Sensor sensor, int accuracy) {} 
   };
   
   private void computeSpeed()
   {
    	Long NOW=new Long(now);
    	Long PREVIOUS=new Long(previous);
    	pas=(NOW.doubleValue()-PREVIOUS.doubleValue())/1000;
    	Vx+=((double)Ax*(double)pas);
    	Dx+=((double)Vx*(double)pas);
    	
    	Vy+=((double)Ay*(double)pas);
    	Dy+=((double)Vy*(double)pas);
    	
    	Vz+=((double)Az*(double)pas);
    	Dz+=((double)Vz*(double)pas);
    	
   }
   
   @Override 
   protected void onResume() 
   { 
        super.onResume(); 
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_UI); 
   } 
    
   @Override 
   protected void onStop() 
   { 
        mSensorManager.unregisterListener(mSensorListener); 
        super.onStop(); 
   } 
    
    
    
 //**********fin de la class*************************   
}